package com.py.credit.controller;

import com.py.credit.constant.ResourcePath;
import com.py.credit.entity.ReportCustom;
import com.py.credit.service.CreditReportCustomService;
import com.py.framework.core.rest.support.RestServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * 信用评价报告模版定制
 *
 * @author YaoS
 * @date 2019-04-24 10:07
 */
@RestController
@RequestMapping("/creditReportConfig")
public class CreditReportCustomController {

    @Autowired
    CreditReportCustomService customService;

    @Value("${absolutepath}")
    private String sysPath;

    @RequestMapping("/addReportElement.json")
    public RestServiceResult<List<Map<String, Object>>> addReportConfig(
            @RequestParam(value = "dept", required = false) String dept,
            @RequestParam(value = "bgbh", required = false) String bgbh,
            @RequestParam(value = "reportStatement", required = false) String reportStatement,
            @RequestParam(value = "reportWatermark", required = false) CommonsMultipartFile reportWatermark,
            @RequestParam(value = "enterpriseIcon", required = false) CommonsMultipartFile enterpriseIcon) throws IOException {
        RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
        String backGround = sysPath + ResourcePath.WATERMAERK_IMAGES_DIR;
        customService.addReportConfig(backGround, dept,reportWatermark,enterpriseIcon,bgbh,reportStatement);
        return result;

    }

    @RequestMapping("/getReportDetail.json")
    public RestServiceResult<ReportCustom> getReportTextElement() {
        RestServiceResult<ReportCustom> result = new RestServiceResult<>();
        ReportCustom latest = customService.getLatest();
        result.setData(latest);
        return result;
    }

    @RequestMapping("/getWatermark.png")
    public void get(HttpServletResponse response,String name) throws IOException{
    	ReportCustom latest = customService.getLatest();
    	if("reportWatermark".equals(name)) {
    		name = latest.getReportWatermark();
    	}else if("enterpriseIcon".equals(name)) {
    		name = latest.getEnterpriseIcon();
    	}
    	this.getWatermark(response, name);
    }
    public void getWatermark(HttpServletResponse response,String name) throws IOException {
        response.setContentType("image/png");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputSream = response.getOutputStream();

        if (new File(sysPath + name).exists()) {

            InputStream fileInputStream = new FileInputStream(sysPath + name);
            byte[] data = toByteArray(fileInputStream);
            fileInputStream.close();

            InputStream in = new ByteArrayInputStream(data);
            in.close();
            int len = 0;

            byte[] buf = new byte[1024];
            while ((len = in.read(buf, 0, 1024)) != -1) {
                outputSream.write(buf, 0, len);
            }

            outputSream.close();
        }
    }

    private byte[] toByteArray(InputStream in) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }
}
